<template src="./template.html"></template>
<style src="./style.css" scoped></style>
<script>
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import notification from "utils/notification";
    import constant from "constants/constant";

    const itemList = [
        "快递无跟踪记录",
        "快递一直未送到",
        "空包裹/少货",
        "未按照规定时间发货",
        "多拍/拍错/不想要",
        "其他"
    ];

    export default{
        data(){
            return{
                goods: {},
                isShow: false,
                itemList: itemList,
                applyType: "修改退款",
                maxRefundValue: null,
                reasonValue: "",
                refundValue: "",
                remarkValue: "",
                errorTxt: null
            }
        },
        created(){
            let self = this;
            let config = weex.config;
            self.goods = config.goods ? config.goods : {};

            //初始化表单
            self.initForm(self.goods);

            //获取可退最大金额
            self.maxRefundValue = self.getmaxRefundValue(self.goods);
        },
        methods: {
            subApplyRefund(){
                let self = this;
                if(self.goods.rid){
                    //修改申请退款
                    self.modifyRefund();
                    return false;
                }

                //申请退款
                self.applyRefund();
            },
            applyRefund(){
                let self = this;
                let params = {
                    ogid: self.goods.ogid,
                    applyreason: self.reasonValue,
                    applymoney: self.refundValue,
                    applytext: self.remarkValue
                };
                if(self.checkForm()){
                    api.sendHttp("goodsRefundApi",params).then(res=>{
                         modal.toast("申请退款成功");
                         notification.send(constant.EVENT_ORDER_REFUND,res.data);
                         navigator.pop(1);
                    },
                    res=>{
                        modal.toast(res.msg || "申请退款失败");
                    });
                }
            },
            modifyRefund(){
                let self = this;
                let params = {
                    type: self.applyType,
                    rid: self.goods.rid,
                    applyreason: self.reasonValue,
                    applymoney: self.refundValue,
                    applytext: self.remarkValue
                };
                if(self.checkForm()){
                    api.sendHttp("refundUpdateApi",params).then(res=>{
                         modal.toast("修改退款成功");
                         notification.send(constant.EVENT_ORDER_MODIFY_REFUND,res.data);
                         navigator.pop(1);
                    },
                    res=>{
                        modal.toast(res.msg || "申请修改退款失败");
                    });
                }
            },
            checkForm(){
                let self = this;
                //验证用户填写信息
                if(!self.reasonValue){
                    self.errorTip("请选择退款原因");
                    return false;
                }

                if(!self.refundValue){
                    self.errorTip("请输入退款金额");
                    return false;
                }

                if(!/^([1-9]\d*(\.\d+)?|0\.\d+)$/.test(self.refundValue)){
                    self.errorTip("请输入有效数字金额");
                    return false;
                }

                if(self.maxRefundValue && self.refundValue > self.maxRefundValue){
                    self.errorTip("输入金额不能超过最大可退金额");
                    return false;
                }

                if(self.remarkValue && self.remarkValue.length && self.remarkValue.length >200){
                    self.errorTip("退款说明不能超过200字");
                    return false;
                }

                return true;
            },
            showReasonClick(){
                this.isShow = true;
            },
            choiceClick(e){
                this.reasonValue = e;
                this.isShow = false;
            },
            errorTip(tip){
                let self = this;
                self.errorTxt = tip;

                if(self.errorTxt) {
                    setTimeout(function(){
                        self.errorTxt = "";
                    },1500);
                }
            },
            initForm(goods){
                let self = this;
                if(goods){
                    if(goods.applyreason){
                        self.reasonValue = goods.applyreason;
                    }

                    if(goods.applymoney){
                        self.refundValue = goods.applymoney;
                    }

                    if(goods.applytext){
                        self.remarkValue = goods.applytext;
                    }
                }
            },
            getmaxRefundValue(goods){
                return goods.g_goods_paymoney;
            }
        }
    }
</script>
